<?php

/**
 * 这是一个表 "sale" 的模型类。
 *
 * 表 'sale' 有以下字段:
 * @property integer $id
 * @property integer $party_id
 * @property string $order_number
 * @property string $product_name
 * @property string $bar
 * @property string $goods_number
 * @property double $discount
 * @property string $add_time
 * @property double $price
 * @property double $sale_count
 * @property double $amount
 * @property string $integral_qty
 * @property string $integral_type
 * 以下是表的关联关系:
 * @property Party $party
 */
class Sale extends CActiveRecord
{
	/**
	 * @return string 表名
	 */
	public function tableName()
	{
		return 'sale';
	}
        
        public $phone;

        /**
	 * @return array 表字段验证规则的数组。
	 */
	public function rules()
	{
		return array(
                    array('party_id', 'numerical', 'integerOnly'=>true),
                    array('discount, price, sale_count, amount', 'numerical'),
                    array('order_number, bar, goods_number', 'length', 'max'=>255),
                    array('product_name,phone', 'length', 'max'=>50),
                    array('integral_qty, integral_type', 'length', 'max'=>20),
                    array('add_time', 'safe'),
                    // 以下规则用于验证查询函数，如果不做查询可注释此行。
                    array('phone,id, party_id, order_number, product_name, bar, goods_number, discount, add_time, price, sale_count, amount, integral_qty, integral_type', 'safe', 'on'=>'search'),
                );
	}

	/**
	 * @return array 关联关系。
	 */
	public function relations()
	{
		return array(
			'party' => array(self::BELONGS_TO, 'Party', 'party_id',),
		);
	}

	/**
	 * @return array 自定义字段显示文本标签 (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '序号',
			'party_id' => '用户',
			'order_number' => '订单编号',
			'product_name' => '商品名',
			'bar' => '条形码',
			'goods_number' => '货号',
			'discount' => '折扣率',
			'add_time' => '消费时间',
			'price' => '单价',
			'sale_count' => '数量',
			'amount' => '总金额',
                        'phone'=>'电话',
                        'integral_qty' => '积分数量',
                        'integral_type' => '积分类型',
		);
	}

	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search()
	{       
		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
		$criteria->compare('party.user_code',$this->party_id,true);
		$criteria->compare('t.order_number',$this->order_number,true);
		$criteria->compare('t.product_name',$this->product_name,true);
		$criteria->compare('t.bar',$this->bar,true);
		$criteria->compare('t.goods_number',$this->goods_number,true);
		$criteria->compare('t.discount',$this->discount);
		$criteria->compare('t.add_time',$this->add_time,true);
		$criteria->compare('t.price',$this->price);
		$criteria->compare('t.sale_count',$this->sale_count);
		$criteria->compare('t.amount',$this->amount);
                $criteria->compare('t.integral_qty',$this->integral_qty,true);
                $criteria->compare('t.integral_type',$this->integral_type,true);                
		$criteria->order='t.id desc';//排序字段
                $criteria->with = 'party';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_clander($party_ids)
	{       
		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
//		$criteria->compare('party.user_code',$this->party_id,true);
		$criteria->compare('t.order_number',$this->order_number,true);
		$criteria->compare('t.product_name',$this->product_name,true);
		$criteria->compare('t.bar',$this->bar,true);
		$criteria->compare('t.goods_number',$this->goods_number,true);
		$criteria->compare('t.discount',$this->discount);
		$criteria->compare('t.add_time',$this->add_time,true);
		$criteria->compare('t.price',$this->price);
		$criteria->compare('t.sale_count',$this->sale_count);
		$criteria->compare('t.amount',$this->amount);
                $criteria->addInCondition('party_id', $party_ids);
		$criteria->order='t.id desc';//排序字段
                $criteria->with = 'party';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}        
        public function getTotals($id)
        {
            //$ids = implode(",",$ids);
            $connection=Yii::app()->db;
            $command=$connection->createCommand("SELECT truncate(SUM(amount),1) FROM `sale` where party_id=".$id);
            return $command->queryScalar();
        }
        public function getTotalsQty($id)
        {
            //$ids = implode(",",$ids);
            $connection=Yii::app()->db;
            $command=$connection->createCommand("SELECT SUM(integral_qty) FROM `sale` where party_id=".$id);
            return $command->queryScalar();
        }        
        
        /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_group()
	{
		$criteria=new CDbCriteria;
             
                $criteria->compare('t.id',$this->id);
		$criteria->compare('party.user_code',$this->party_id,true);
		$criteria->compare('t.order_number',$this->order_number,true);
		$criteria->compare('t.product_name',$this->product_name,true);
		$criteria->compare('t.bar',$this->bar,true);
		$criteria->compare('t.goods_number',$this->goods_number,true);
		$criteria->compare('t.discount',$this->discount);
		$criteria->compare('t.add_time',$this->add_time,true);
		$criteria->compare('t.price',$this->price);
		$criteria->compare('t.sale_count',$this->sale_count);
		$criteria->compare('t.amount',$this->amount);
//                $criteria->compare('party.phone',$this->phone);
		$criteria->order='t.id desc';//排序字段
                $criteria->group = 'party.user_code';
                $criteria->with = 'party';
                $criteria->addCondition("party.phone like'".$this->phone."%'");
               

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
        /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_export()
	{
		$criteria=new CDbCriteria;
             

//                $criteria->compare('party.phone',$this->phone);
                $criteria->select = 't.integral_type as  integral_type,sum(t.integral_qty) as integral_qty,party.phone as phone';
                $criteria->group = 't.party_id,t.integral_type';
		$criteria->order='t.id desc';//排序字段
                $criteria->with = 'party';
                $criteria->addCondition("party.phone like'".$this->phone."%'");
                return Sale::model()->findAll($criteria);
                
//		return new CActiveDataProvider($this, array(
//			'criteria'=>$criteria,
//			'pagination'=>array(
//                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
//            ),
//		));
	}
        /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_groups($party_ids)
	{
		$criteria=new CDbCriteria;
             
                $criteria->compare('t.id',$this->id);
//		$criteria->compare('party.user_code',$this->party_id,true);
		$criteria->compare('t.order_number',$this->order_number,true);
		$criteria->compare('t.product_name',$this->product_name,true);
		$criteria->compare('t.bar',$this->bar,true);
		$criteria->compare('t.goods_number',$this->goods_number,true);
		$criteria->compare('t.discount',$this->discount);
		$criteria->compare('t.add_time',$this->add_time,true);
		$criteria->compare('t.price',$this->price);
		$criteria->compare('t.sale_count',$this->sale_count);
		$criteria->compare('t.integral_qty',$this->integral_qty);
                $criteria->compare('t.integral_type',$this->integral_type);
                $criteria->compare('t.sale_count',$this->sale_count);
                $criteria->addInCondition('party_id', $party_ids);
//                $criteria->compare('party.phone',$this->phone);
		$criteria->order='t.id desc';//排序字段
                $criteria->group = 't.order_number';
                $criteria->with = 'party';
                $criteria->addCondition("party.phone like'".$this->phone."%'");
               

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}        
        /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_party($party_id)
	{
		$criteria=new CDbCriteria;
             
                $criteria->compare('t.id',$this->id);
		$criteria->compare('t.party_id',$party_id);
		$criteria->compare('t.order_number',$this->order_number,true);
		$criteria->compare('t.product_name',$this->product_name,true);
		$criteria->compare('t.bar',$this->bar,true);
		$criteria->compare('t.goods_number',$this->goods_number,true);
		$criteria->compare('t.discount',$this->discount);
		$criteria->compare('t.add_time',$this->add_time,true);
		$criteria->compare('t.price',$this->price);
		$criteria->compare('t.sale_count',$this->sale_count);
		$criteria->compare('t.amount',$this->amount);
//                $criteria->compare('party.phone',$this->phone);
		$criteria->order='t.id desc';//排序字段
                $criteria->group = 't.order_number';
                $criteria->with = 'party';
                $criteria->addCondition("party.phone like'".$this->phone."%'");
               

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Sale the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
